//
//  StrategyViewController.m
//  Community
//
//  Created by YaSha_Tom on 2018/12/20.
//  Copyright © 2018年 YaSha-Tom. All rights reserved.
//

#import "StrategyViewController.h"
#import "StrategyTableViewCell.h"
#import "SceneryViewController.h"
#import "DataManager.h"
#import <AFNetworking.h>
#import <QMUIKit.h>

@interface StrategyViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSArray *dataSource;

@end

@implementation StrategyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = [NSString stringWithFormat:@"%@之旅",self.titleStr];
    self.view.backgroundColor = [UIColor whiteColor];
    [self doNetworking];
    [self initUI];
}
- (void)initUI {
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 375, 667) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 120;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    StrategyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if(cell == nil){
        cell = [[StrategyTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;//取消cell 的点击效果
    FoundModel *model = self.dataSource[indexPath.row];
    [cell setStrategyData:model];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    SceneryViewController *sceneryViewController = [[SceneryViewController alloc]init];
    FoundModel *model = self.dataSource[indexPath.row];
    sceneryViewController.model = model;
    sceneryViewController.titleName = [NSString stringWithFormat:@"%@攻略",model.name];
    [self.navigationController pushViewController:sceneryViewController animated:YES];
}
- (void)doNetworking {
    // 1.创建请求管理对象
    AFHTTPSessionManager *manger = [AFHTTPSessionManager manager];
    NSString *url =[NSString stringWithFormat:@"http://q.chanyouji.com/api/v1/search.json?q=%@&search_type=destination",self.id];
    NSLog(@"------%@",url);
    [manger GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"成功%@",responseObject);
        if ([responseObject[@"status"]  isEqual: @200]) {
            self.dataSource = [DataManager getAddressData:responseObject[@"data"]];
            [self.tableView reloadData];

        }else{
            [QMUITips showSucceed:@"请求数据失败" inView:self.view hideAfterDelay:1];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"失败%@",error);
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
